package com.example.buildtable.controller;

import com.example.common.util.R;
import com.example.buildtable.pojo.Handle;
import com.example.buildtable.service.HandleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "处理")
@RequestMapping(value = "/handle/")
public class HandleController {
    @Autowired
    private HandleService handleService;
    
    @Operation(summary = "查询所有处理数据")
    @GetMapping(value = "/getAllHandle")
    public R getAllHandle() {
        List<Handle> handleList = handleService.list();
        return R.ok("查询所有处理数据成功").data("handleList", handleList);
    }
    
    /*@Operation(summary = "登录处理")
    @RequestMapping*/
    
    @Operation(summary = "注册处理")
    @PostMapping(value = "/addHandle")
    public R addHandle(Handle handle) {
        boolean flag = handleService.save(handle);
        if (flag) {
            return R.ok("处理注册成功");
        } else {
            return R.ok("处理注册失败");
        }
    }

    @Operation(summary = "注销处理")
    @GetMapping(value = "/deleteHandle/{handleId}")
    public R deleteHandle(@PathVariable("handleId") Integer handleId) {
        boolean flag = handleService.removeById(handleId);
        if (flag) {
            return R.ok("处理注销成功");
        } else {
            return R.ok("处理注销失败");
        }
    }

    @Operation(summary = "按照处理查看")
    @GetMapping(value = "/getHandleById/{handleId}")
    public R getHandleById(@PathVariable("handleId") Integer handleId) {
        Handle handle = handleService.getById(handleId);
        if (handle != null) {
            return R.ok("按照处理查看成功").data("handle", handle);
        } else {
            return R.error("按照处理查看失败");
        }
    }

    @Operation(summary = "修改处理")
    @PostMapping(value = "/updateHandle")
    public R updateHandle(Handle handle) {
        boolean flag = handleService.updateById(handle);
        if (flag) {
            return R.ok("处理修改成功");
        } else {
            return R.ok("处理修改失败");
        }
    }
}